package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.cache.AlarmConfigCache;
import com.ruoyi.system.domain.SysDevice;
import com.ruoyi.system.mapper.SysDeviceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysAlarmConfigMapper;
import com.ruoyi.system.domain.SysAlarmConfig;
import com.ruoyi.system.service.ISysAlarmConfigService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 告警配置Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-08-13
 */
@Service
public class SysAlarmConfigServiceImpl implements ISysAlarmConfigService 
{
    @Autowired
    private SysAlarmConfigMapper sysAlarmConfigMapper;

    @Autowired
    private AlarmConfigCache alarmConfigCache;

    @Autowired
    private SysDeviceMapper sysDeviceMapper;
    /**
     * 查询告警配置
     * 
     * @param alarmConfigId 告警配置主键
     * @return 告警配置
     */
    @Override
    public SysAlarmConfig selectSysAlarmConfigByAlarmConfigId(Long alarmConfigId)
    {
        return sysAlarmConfigMapper.selectSysAlarmConfigByAlarmConfigId(alarmConfigId);
    }

    /**
     * 查询告警配置列表
     * 
     * @param sysAlarmConfig 告警配置
     * @return 告警配置
     */
    @Override
    public List<SysAlarmConfig> selectSysAlarmConfigList(SysAlarmConfig sysAlarmConfig)
    {
        return sysAlarmConfigMapper.selectSysAlarmConfigList(sysAlarmConfig);
    }

    /**
     * 新增告警配置
     * 
     * @param sysAlarmConfig 告警配置
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSysAlarmConfig(SysAlarmConfig sysAlarmConfig)
    {
        sysAlarmConfig.setCreateTime(DateUtils.getNowDate());
        int row = sysAlarmConfigMapper.insertSysAlarmConfig(sysAlarmConfig);
        //刷新缓存
        alarmConfigCache.refreshConfig(sysAlarmConfig.getAlarmConfigId());
        //配置报警设备
        sysDeviceMapper.configAlarm(sysAlarmConfig.getAlarmConfigId(),sysAlarmConfig.getDeviceIds());
        return row;
    }

    /**
     * 修改告警配置
     * 
     * @param sysAlarmConfig 告警配置
     * @return 结果
     */
    @Override
    @Transactional
    public int updateSysAlarmConfig(SysAlarmConfig sysAlarmConfig)
    {
        //重置告警配置
        SysDevice bind = new SysDevice();
        bind.setAlarmConfigId(sysAlarmConfig.getAlarmConfigId());
        List<SysDevice> bindDevices = sysDeviceMapper.selectSysDeviceList(bind);
        sysDeviceMapper.configAlarm(0L ,bindDevices.stream().map(SysDevice::getDeviceId).collect(Collectors.toList()));

        sysAlarmConfig.setUpdateTime(DateUtils.getNowDate());
        int row = sysAlarmConfigMapper.updateSysAlarmConfig(sysAlarmConfig);
        //刷新缓存
        alarmConfigCache.refreshConfig(sysAlarmConfig.getAlarmConfigId());
        //配置报警设备
        sysDeviceMapper.configAlarm(sysAlarmConfig.getAlarmConfigId(),sysAlarmConfig.getDeviceIds());
        return row;
    }

    /**
     * 批量删除告警配置
     * 
     * @param alarmConfigIds 需要删除的告警配置主键
     * @return 结果
     */
    @Override
    public int deleteSysAlarmConfigByAlarmConfigIds(Long[] alarmConfigIds)
    {
        return sysAlarmConfigMapper.deleteSysAlarmConfigByAlarmConfigIds(alarmConfigIds);
    }

    /**
     * 删除告警配置信息
     * 
     * @param alarmConfigId 告警配置主键
     * @return 结果
     */
    @Override
    public int deleteSysAlarmConfigByAlarmConfigId(Long alarmConfigId)
    {
        //重置告警配置
        SysDevice bind = new SysDevice();
        bind.setAlarmConfigId(alarmConfigId);
        List<SysDevice> bindDevices = sysDeviceMapper.selectSysDeviceList(bind);
        if (!bindDevices.isEmpty()){
            sysDeviceMapper.configAlarm(0L ,bindDevices.stream().map(SysDevice::getDeviceId).collect(Collectors.toList()));
        }
        int row = sysAlarmConfigMapper.deleteSysAlarmConfigByAlarmConfigId(alarmConfigId);
        alarmConfigCache.removeConfig(alarmConfigId);
        return row;
    }
}
